package com.woniu.his.mms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.his.mms.client.DrugStoreClient;
import com.woniu.his.mms.client.param.PatientParam;
import com.woniu.his.mms.client.param.RefundDrugParam;
import com.woniu.his.mms.client.result.PatientResult;
import com.woniu.his.mms.client.param.ReleaseDrugParam;
import com.woniu.his.mms.common.MmsResultCode;
import com.woniu.his.mms.service.DrugService;
import com.woniu.his.mms.service.dto.PatientDto;
import com.woniu.his.mms.service.param.PatienServiceParam;
import com.woniu.his.mms.service.param.RefundDrugServiceParam;
import com.woniu.his.mms.service.param.ReleaseDrugServiceParam;
import com.woniu.his.result.CommonResultCode;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @version: java version 1.8
 * @Author: Mr tao
 * @description:
 * @date: 2022-11-29 16:01
 */
@Slf4j
@RestController
@RequestMapping("mms")
public class DrugStoreApiImpl implements DrugStoreClient {

    @Resource
    private DrugService drugService;

    /**
     * 刷新拿药患者
     *
     * @param patientParam
     * @return
     */
    @Override
    public Result<PatientResult> listPatient(PatientParam patientParam) {
        log.info("刷新拿药患者:{}", patientParam);
        //类型转换
        PatienServiceParam param = BeanUtil.copyProperties(patientParam, PatienServiceParam.class);
        PatientDto dto = drugService.listPatient(param);
        return Result.getInstance(PatientResult.class).setData(BeanUtil.copyProperties(dto, PatientResult.class));
    }

    /**
     * 发药
     *
     * @param releaseDrugParam
     * @return
     */
    @Override
    public Result releaseDrug(ReleaseDrugParam releaseDrugParam) {
        log.info("开始发药:{}", releaseDrugParam);
        //类型转换
        ReleaseDrugServiceParam param = BeanUtil.copyProperties(releaseDrugParam, ReleaseDrugServiceParam.class);
        boolean b = drugService.releaseDrug(param);
        return b ? Result.getInstance() : Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("发药失败");
    }

    /**
     * 退药
     *
     * @param refundDrugParam
     * @return
     */
    @Override
    public Result refundDrug(RefundDrugParam refundDrugParam) {
        log.info("退药:{}", refundDrugParam);
        RefundDrugServiceParam param = BeanUtil.copyProperties(refundDrugParam, RefundDrugServiceParam.class);
        boolean b = drugService.refundDrug(param);
        return b ? Result.getInstance().setMsg("退药成功") :
                Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("退药失败");
    }
}
